package com.example.dairenkun.neufoodshop.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.dairenkun.neufoodshop.R;
import com.example.dairenkun.neufoodshop.adapter.SearchListAdapter;
import com.example.dairenkun.neufoodshop.bean.Food;
import com.example.dairenkun.neufoodshop.model.SearchModel;
import com.example.dairenkun.neufoodshop.service.SearchService;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class SearchFragment extends Fragment {


    private RecyclerView recyclerView;
    private View view=null;
    private Button btn_search;
    private EditText search_content;
    public SearchFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view= inflater.inflate(R.layout.fragment_search, container, false);
        initViews();
        initEvents();
        return view;
    }
    private  void initViews(){
        btn_search=(Button)view.findViewById(R.id.btn_search);
        search_content=(EditText)view.findViewById(R.id.search_content);
        recyclerView=(RecyclerView)view.findViewById(R.id.recycler_Search_id);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
    }
    private  void initEvents(){
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), search_content.getText().toString(), Toast.LENGTH_SHORT).show();
                //获取网上的数据/
               /* Retrofit retrofit=new Retrofit.Builder()
                        .baseUrl("http://60.205.189.39/ ")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                SearchService Service=retrofit.create(SearchService.class);
                Call<List<Food>> call=Service.getsearchList(search_content.getText().toString());*/
                SearchModel searchModel=new SearchModel();
                Call <List<Food>>call=searchModel.Search(search_content.getText().toString());
                call.enqueue(new Callback<List<Food>>() {
                    @Override
                    public void onResponse(Call<List<Food>> call, retrofit2.Response<List<Food>> response)
                    {
                        List<Food> food=response.body();
                        recyclerView.setAdapter(new SearchListAdapter(getActivity(),food));
                    }
                    @Override
                    public void onFailure(Call<List<Food>> call, Throwable t) {

                    }
                });
            }
        });
    }
}
